﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace SAHCPIMS_final
{
    public partial class PRENATAL : Form
    {
        public int familyNumber;
        public string firstName;
        public string middleName;
        public string surname;
        public string memberBDate;
        public int weight;
        public string checkUpType;

        public string homeEmpName;
        public int homeEmpNum;

        public int doFlag = 0;
        string tox = null;

        public PRENATAL()
        {
            InitializeComponent();
        }

        private void PRENATAL_Load(object sender, EventArgs e)
        {
            this.familyNumberMismo.Text = familyNumber.ToString();
            this.familyInformationLabel.Text = firstName + " " + middleName + " " + surname;
            this.bDateInfo.Text = memberBDate;
            this.weightInfo.Text = weight.ToString()+" kg";
            this.checkupTypeInfo.Text = "Prenatal";
            this.checkupDate.Text = DateTime.Now.ToShortTimeString();

            this.employeeLabel.Text = homeEmpName;
            this.empNumLabel.Text = homeEmpNum.ToString();
        }

        private void logOutButton_Click(object sender, EventArgs e)
        {
            LOGIN logout = new LOGIN();
            logout.Visible = true;
            this.Visible = false;
        }

        private void acceptMemberButton_Click(object sender, EventArgs e)
        {
            /** Connection String: Provider of the connection and the source of data **/
            string conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\\SAHCPIMS_database.mdb";

            /** create instance of the connection with the connection string as parameter **/
            OleDbConnection conn = new OleDbConnection(conString);

            /** 
             * Creating an instance of the DataSet which represents an in-memory cache of data
             * This is where the result of the database query will be stored
            **/
            DataSet ds = new DataSet();
            OleDbDataAdapter tagalagay;
            conn.Open();

            //for connection to the database

            if (ToxoidCheckBox.Checked)
            {
                try
                {
                    tagalagay = new OleDbDataAdapter("INSERT into ToxoidPrenatal "
                    + "values('" + familyNumber + "','" + firstName + "','" + middleName + "','" + surname + "','" + int.Parse(gravidatxtbx.Text) + "','" + DateTime.Now.ToShortDateString() + "')", conn);
                    tagalagay.Fill(ds);
                    tox = DateTime.Now.ToShortDateString();
                    doFlag = 0;
                }
                catch (OleDbException)
                {
                    MessageBox.Show("Data not Saved in Database. There's a Problem with the Input (it is either it already exists, nothing in input or wrong format in input).");
                    doFlag = 1;

                }
            }
            else { ToxoidLabel.Text = "Not Yet Given"; tox = null; }

            try
            {
                tagalagay = new OleDbDataAdapter("INSERT into Prenatal "
                                          + "values('" + familyNumber + "','" + firstName + "','" + middleName + "','" + surname + "','" + DateTime.Now.ToShortDateString() + "','" + this.xpctddlvrytxtbx.Text + "','" + int.Parse(aogtxtbx.Text) + "','" + this.LMPtxtbx.Text + "','" + int.Parse(gravidatxtbx.Text) + "','" + int.Parse(paratxtbx.Text) + "')", conn);
                tagalagay.Fill(ds);
                doFlag = 0;
            }

        //catches exception if the user input is invalid
            catch (FormatException)
            {

                MessageBox.Show("Message: Invalid Input. Try Again.");
                doFlag = 1;

            }
            //catches exception if the input is already in the database or the input is not saved in the database
            catch (OleDbException)
            {
                MessageBox.Show("Data not Saved in Database. There's a Problem with the Input (it is either it already exists, nothing in input or wrong format in input).");
                doFlag = 1;

            }

            //closes database
            conn.Close();

            //if registration is successful, the form is disabled
            if (doFlag == 0)
            {

                EDDlabel.Text = this.xpctddlvrytxtbx.Text;
                AOGlabel.Text = this.aogtxtbx.Text;
                GravLabel.Text = this.gravidatxtbx.Text;
                parLabel.Text = this.paratxtbx.Text;
                LMPlabel.Text = this.LMPtxtbx.Text;
                ToxoidLabel.Text = tox;

                groupBox1.Visible = false;
                success.Visible = true;

            }
        }

        private void homebutton_Click(object sender, EventArgs e)
        {
            HOME home = new HOME();
            home.homeEmpName = homeEmpName;
            home.homeEmpNum = homeEmpNum;
            home.Visible = true;
            this.Visible = false;
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            HOME home = new HOME();
            home.homeEmpName = homeEmpName;
            home.homeEmpNum = homeEmpNum;
            home.Visible = true;
            this.Visible = false;
        }
    }
}
